package wangy.pvp.scanner.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import wangy.pvp.scanner.Scanner;
import wangy.pvp.thread.ProduceThread;

public abstract class ScannerManager extends ProduceThread implements Scanner {

	private String initialAddress;

	protected ScannerImpl scanner;
	protected FileTracer fTracer;

	List<File> sourceFileList;
	int index = 0;

	protected ScannerManager(String initialAddress){

		this.initialAddress = initialAddress;
		sourceFileList = new ArrayList<File>();

	}

	public void selectFiles(){

		if(sourceFileList.size() == 0){
			File sourceFile = new File(initialAddress);
			fTracer.getSourceFiles(sourceFile, sourceFileList);
		}
		index = 0; 

	}

	public void startAnnotationScanner(){
		this.setAnnotationScanner();
	}
	
	public void startMainBodyScanner(){
		this.setBodyScanner();
	}
	
	public void startAllScanner(){
		this.setAllScanner();
	}

	public String getNext(){
		
		if(index<this.sourceFileList.size())
			return scanner.scanFile(this.sourceFileList.get(index++));
		return  null;
	}
	
	public String getNextSentece(){
		
		String temp = null;
		
		if(index<this.sourceFileList.size()){	
			temp = scanner.ScanSentence(this.sourceFileList.get(index));
			if(temp.equals("end"))
				if(++index<this.sourceFileList.size())
					temp = scanner.ScanSentence(this.sourceFileList.get(index));
				else
					temp = "end";			
		}
		
		return temp;
	}
	
	public boolean isEmpty(){
		
		if(this.sourceFileList.size() == 0)
			return true;
		return false;
		
	}
	
	public String getAddress(){
		return this.initialAddress;
	}
	
	public Iterator<File> getFileList(){
		return sourceFileList.iterator();
	}
	
	public String produceStr(){
		return this.getNextSentece();
	}
	
	abstract protected void setAnnotationScanner();
	
	abstract protected void setBodyScanner();
	
	abstract protected void setAllScanner();
	
}
